﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using LuaInterface;

namespace CodeGame
{
    internal class HookObject
    {
        public virtual void Run(string arg1, string arg2, string arg3)
        {
            Console.WriteLine(arg1);
            Console.WriteLine(arg2);
            Console.WriteLine(arg3);
        }
    }

    internal class LuaHook : HookObject
    {
        private LuaFunction Function;

        public LuaHook(LuaFunction func)
        {
            this.Function = func;
        }

        public override void Run(string arg1, string arg2, string arg3)
        {
            this.Function.Call(arg1, arg2, arg3);
        }
    }

    public class HookAPI
    {
        public delegate void CHook(string arg1, string arg2, string arg3);
    }

    internal class CSharpHook : HookObject
    {
        private HookAPI.CHook HookObj;

        public CSharpHook(HookAPI.CHook hook)
        {
            this.HookObj = hook;
        }

        public override void Run(string arg1, string arg2, string arg3)
        {
            this.HookObj(arg1, arg2, arg3);
        }
    }

    internal class HookManager
    {
        public Dictionary<string, List<HookObject>> Hooks = new Dictionary<string, List<HookObject>>();

        public void Call(string name, string arg1, string arg2, string arg3)
        {
            if (this.Hooks.ContainsKey(name))
            {
                foreach (HookObject item in this.Hooks[name])
                {
                    item.Run(arg1, arg2, arg3);
                }
            }
        }

        public void Add(string hook, HookObject obj)
        {
            if (!this.Hooks.ContainsKey(hook))
            {
                this.Hooks.Add(hook, new List<HookObject>());
            }
            this.Hooks[hook].Add(obj);
        }

        public void Add(string hook, LuaFunction func)
        {
            this.Add(hook, new LuaHook(func));
        }

        public void Add(string hook, HookAPI.CHook func)
        {
            this.Add(hook, new CSharpHook(func));
        }

    }
}
